package tzy.viewpager;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Created by Administrator on 2017/11/28 0028.
 */

public abstract class MyPagerAdapter<T> extends PagerAdapter {
    protected T[] mData;
    private Deque<View> mCaches = new ArrayDeque<>(3);

    public MyPagerAdapter(T[] data) {
        mData = data;
    }

    public void refreshData(T[] data) {
        mData = data;
        notifyDataSetChanged();
    }


    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        mCaches.add((View) object);
//                container.removeView((View) object);
        container.removeView((View) object);
    }

    public final int getRealPosition(int position) {
        final int count = getRealCount();

        return count == 0 ? -1 : position < 0 ? -1 : position % getRealCount();
    }

    @Override
    public final Object instantiateItem(ViewGroup container, int position) {
        View view = getView(container, mCaches.pollFirst(), getRealPosition(position));
        container.addView(view);
        return view;
    }

    public View getView(ViewGroup container, View convertView, int position) {

        return null;
    }


    public T getItem(int realPosition) {
        return mData[realPosition];
    }

    @Override
    public final int getCount() {
        final int count = getRealCount();
        if (count > 1) {
            return Integer.MAX_VALUE;
        }

        return count;
    }

    protected OnPageItemClickListener onPageItemClickListener;

    public void setOnPageItemClickListener(OnPageItemClickListener onPageItemClickListener) {
        this.onPageItemClickListener = onPageItemClickListener;
    }

    public interface OnPageItemClickListener {
        void onPageItemClick(ViewPager viewPager, View view, int position);
    }


    public int getRealCount() {
        return mData == null ? 0 : mData.length;
    }


    public abstract long getScrollInterval();

    public abstract int getCurrentPointResource();

    public abstract int getOtherPointResource();
}
